package com.woniuxy.oa.dao;

import com.woniuxy.oa.dao.provider.RoleProvider;
import com.woniuxy.oa.entity.Permission;
import com.woniuxy.oa.entity.Role;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

import java.util.List;


public interface RoleDAO {
    /**
     * 增加角色
     *
     * @param role
     */
    @Insert("insert into oa_role values(#{id},#{roleName},#{status},#{description})")
    public void add(Role role);

    /**
     * 通过id逻辑删除角色
     *
     * @param id
     */
    @Update("update oa_role set status=#{status} where id=#{id}")
    public void delById(String status, String id);

    /**
     * 修改角色
     *
     * @param role
     */
    @Update("update oa_role set rolename=#{roleName},status=#{status},description=#{description} where id=#{id}")
    public void update(Role role);

    /**
     * 得到所有角色
     *
     * @return
     */
    @Select("select * from oa_role")
    public List<Role> getRoles();

    /**
     * 通过id查询角色
     *
     * @param id
     * @return
     */
    @Select("select * from oa_role where id=#{id}")
    public Role getById(String id);

    /**
     * 条件查询
     *
     * @param role
     * @return
     */
    @SelectProvider(type = RoleProvider.class, method = "getSQL")
    public List<Role> getAll(Role role);

    /**
     * 权限查询
     * @param id
     * @return
     */
    @Select("select p.* from oa_role r,oa_permission p,oa_role_perm rp where r.`id`=rp.`roleid` and rp.`permid`=p" +
            ".`id` and r.`id`=#{id}")
    public List<Permission> getPerm(String id);

    @Select("select count(*) from oa_role where oa_role.roleName=#{roleName}")
    public int getCountByName(String name);
}
